eval 'exec perl -wS $0 ${1+"$@"}'
 if 0;

use strict;
use warnings;
use POSIX qw(strftime);

use DBI();
use File::Basename;
use File::stat;
use Data::Dumper 'Dumper';

use IO::Socket;

sub print_h_content
{
    my $hash=shift @_;
    
    foreach my $thekey (keys %$hash)
    {
	print $thekey." => ".$hash->{$thekey}."\n";
    }
}

sub slsecd_connect
{
    my $socket=shift @_;
    my $proto=shift @_;
    my $peeraddr=shift @_;
    my $peerport=shift @_;
    
    $$socket=IO::Socket::INET->new( Proto     => $$proto,
				      PeerAddr  => $$peeraddr,
				      PeerPort  => $$peerport
				      );
    unless ($$socket)
    {
	return 0;
    }
    return 1;
}

sub slsecd_disconnect
{
    my $socket=shift @_;
    
    close $$socket;
}

sub get_deciphered_data
{
    my $h_ref_enciphered_keys = shift @_;
    #my $ref_deciphered_keys = shift @_;
    my $res;
    my $socket=0;
    my $proto="tcp";
    #my $peeraddr="172.24.61.137";
    my $peeraddr="127.0.0.1";
    my $peerport="30000";

    #print ">get_deciphered_data<";
    if (! slsecd_connect(\$socket,\$proto,\$peeraddr,\$peerport))
    {
	error("Failed to connect to slsecd.\n");
	return 4;
    }
    
    foreach my $thekey (keys %$h_ref_enciphered_keys)
    {
	#print $thekey;
	if ($h_ref_enciphered_keys->{$thekey}=~/KO/)
	{
	    #chop ($thekey);
	    print "$thekey;KO;\n";
	    #push(@$ref_deciphered_keys,"$thekey;KO");
	}
	else
	{
	    #print { $h_ref_param->{SOCKET} } "D;" .$thekey. ";" .$h_ref_enciphered_keys->{$thekey}. "\n";
	    print $socket "D;" .$thekey. ";" .$h_ref_enciphered_keys->{$thekey}. "\n";

	    $res = <$socket>;
	    print $res;
	    #chomp($res);
	    #push(@$ref_deciphered_keys,$res);
	}
    }

    #print { $h_ref_param->{SOCKET} } "Q\n";
    print $socket "Q\n";
    if (! slsecd_disconnect(\$socket))
    {
	warning("Failed to disconnect from slsecd.\n");
    }
    
    return 1;
}

#my $myhash=myhash();
#print $myhash."\n"
#get_deciphered_data(%hash);

my %encipherd_list=();

%encipherd_list = (
	 '352793010540494' => 'BA4B3FF567A9259789F0FB30A2E2472D8146B82AC80B5369BAB64ACF9461040A2E92D2EF',
	 '353836010488849' => '2853267031A54642B6B96F2F9033C43F0B0426BC4E06130AFCA9',
         '356319006138675' => 'FF549F70514FD48915D0E5F654441BC5',
	 '357180011629689' => '8FDD228283EA03E2380544AA11CE43D724C14D334833CA5FEC4B',
	 '359569000189432' => '878AFB7D8AF7DCDC21DF999BEDD84D69',
	 '359831004281106' => '6986D1C5E9260ED5B9E6204C7544FFC9',
	 '352315011675838' => 'DE0D19247375DCB6F75C90FAEB7662DB',	   
         '357180011707741' => '3713D2AD6D89E187E6DB3F437CF79EB7',
	 '011417012139740' => '354D30978AF6BB8B1792C5C312E8480E',
         '352315011675838' => 'DE0D19247375DCB6F75C90FAEB7662DB',
	 '332021536074029' => '962A47E6A26C7059F9FB5987DA01B8B17CDBA43446EFC3599FE19EF3D940B417D49093A4',	   
	 '330179531144700' => 'EBFA07EF87C41B76C00E433FC6DB271A',
         '356319002946022' => 'B59F9D5384C838C3849D7856ACC17766',
         '332132671544211' => '0D06128EEA876225F4273007CAD70138DFF6F96BCD3829E4CDE0'
		   );

#print_h_content(\%hash);
get_deciphered_data(\%encipherd_list);
